//
//  ProgressResponse+Ex.swift
//  
//
//  Created by 孙长坦 on 2022/7/28.
//

import Foundation
import Alamofire
import Moya

public extension ProgressResponse {
    func filter<R: RangeExpression>(statusCodes: R) throws -> ProgressResponse where R.Bound == Int {
        guard let response = response else {
            return self
        }
        
        guard statusCodes.contains(response.statusCode) else {
            throw MoyaError.statusCode(response)
        }
        
        return self
    }
    
    func filter(statusCode: Int) throws -> ProgressResponse {
        try filter(statusCodes: statusCode...statusCode)
    }
    
    func filterSuccessfulStatusCodes() throws -> ProgressResponse {
        try filter(statusCodes: 200...299)
    }
    
    func filterSuccessfulStatusAndRedirectCodes() throws -> ProgressResponse {
        try filter(statusCodes: 200...399)
    }
}
